<template>
  <!-- 地址 -->
  <div>
    <div id="top">
      <h2>新增地址</h2>
      <p>
        <span class="iconfont">&#xe64a;</span>
        <span class="iconfont">&#xe614;</span>
        <i class="line"></i>
      </p>
    </div>
    <div class="main">
      <van-address-edit
        :area-list="areaList"
        show-postal
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="info"
        @save="onSave"
        @delete="onDelete"
        @change-detail="onChangeDetail"
      />
    </div>
  </div>
</template>

  <script>
import Vue from "vue";
import { mapGetters, mapActions } from "vuex";
import { AddressEdit } from "vant";
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import {
  addressAdd,
  addressList,
  addressRemove,
  addressEdit,
} from "../../utils/api";
Vue.use(AddressEdit);
export default {
  props: [],
  components: {},
  data() {
    return {
      users: {},
      info: {},
      areaList,
      searchResult: [],
    };
  },
  computed: {
    ...mapGetters({}),
  },
  methods: {
    ...mapActions({}),
    onSave(content) {
      if (this.$route.query.id) {
        //编辑
        addressEdit({
          id: this.$route.query.id,
          username: content.name,
          userphone: content.tel,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
        }).then((res) => {
          // console.log(res);
          if (res.data.code == 200) {
            this.$router.push("/addressgl");
            Toast("编辑成功");
          }
        });
      } else {
        //添加
        addressAdd({
          uid: this.users.uid,
          username: content.name,
          userphone: content.tel,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
        }).then((res) => {
          if (res.data.code == 200) {
            this.$router.push("/addressgl");
            Toast("添加成功");
          }
        });
      }
    },
    onDelete() {
      //删除
      addressRemove({ id: this.$route.query.id }).then((res) => {
        //console.log(res);
        if (res.data.code == 200) {
          this.$router.push("/addressgl");
          Toast("删除成功");
        }
      });
    },
    onChangeDetail() {},
  },
  mounted() {
    this.users = sessionStorage.getItem("user")
      ? JSON.parse(sessionStorage.getItem("user"))
      : {};
    //console.log(this.$route.query.index);
    let index = this.$route.query.index;
    if (this.$route.query.index) {
      //获取一条信息
      addressList({ uid: this.users.uid }).then((res) => {
        //console.log(res.data.list);
        let lists = res.data.list[index] ? res.data.list[index] : [];
        this.info = {
          ...lists,
          name: lists.username,
          tel: lists.userphone,
          addressDetail: lists.useraddress,
          //  province: lists.location,
        };
        // console.log(this.info);
      });
    }
  },
};
</script>

<style scoped>
body,
html {
  height: 100%;
}
body {
  display: flex;
  flex-direction: column;
  max-width: 7.5rem;
  margin: 0 auto;
}
#top {
  height: 0.64rem;
  background: #ff6040;
  display: flex;
  padding: 0.28rem 0.12rem 0.28rem 0.32rem;
  color: #ffdfd8;
  align-items: center;
}
#top h2 {
  margin: 0 1.23rem 0 2.53rem;
  font-size: 0.32rem;
}
#top .arrow_l {
  font-size: 0.34rem;
}
#top p {
  width: 1.74rem;
  height: 0.64rem;
  border: 1px solid #ff8a6f;
  box-sizing: border-box;
  border-radius: 0.54rem;
  display: flex;
  align-items: center;
  position: relative;
}
#top p span {
  width: 50%;
  height: 100%;
  text-align: center;
  line-height: 0.64rem;
}
#top p .line {
  width: 1px;
  height: 0.37rem;
  background: #ff896e;
  position: absolute;
  top: 0.12rem;
  left: 0.86rem;
}
.main .address_dz {
  background-color: #fff;
}
.main textarea {
  resize: none;
  background-color: rgb(248, 248, 248);
  vertical-align: middle;
}
.main .inputa {
  border: none;
}
.main .p1 {
  border-bottom: 1px solid #333;
  padding: 0.2rem 0;
  box-sizing: border-box;
}
.main .biaoqian {
  margin-top: 0.1rem;
  background-color: #fff;
}
.main .inputb {
  margin-left: 0.7rem;
}
.main .p2 {
  margin: 0.3rem 0.1rem;
}
.main .p3 {
  margin: 0.2rem 0.1rem;
}
.main form {
  margin-top: 0.2rem;
  background-color: #fff;
}
.main .input {
  width: 100%;
  height: 0.7rem;
  text-align: center;
  border-radius: 0.1rem;
  outline: none;
  border: none;
}
.main .input1 {
  color: #fff;
  background-color: rgb(165, 6, 6);
}
.main .input2 {
  color: #333;
  background-color: rgb(248, 248, 248);
  border: 1px solid #ccc;
}
</style>